package com.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.constant.MessageConstant;
import com.reggie.constant.StatusConstant;
import com.reggie.dto.DishDTO;
import com.reggie.dto.DishPageQueryDTO;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import com.reggie.entity.SetmealDish;
import com.reggie.exception.BaseException;
import com.reggie.exception.DeletionNotAllowedException;
import com.reggie.mapper.DishFlavorMapper;
import com.reggie.mapper.DishMapper;
import com.reggie.mapper.SetmealDishMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.DishService;
import com.reggie.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 菜品分页功能
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO pageQueryDTO) {

        //设置分页参数
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        //调用mapper实现分页功能
        Page<DishVO> page = dishMapper.fingPage(pageQueryDTO);
        //设置返回值
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 菜品停售起售
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        dishMapper.startOrStop(status,id);

        //判断传过来的状态是否是停售中，如果是，则也把菜品和套餐关联的套餐也停售
        if (status == StatusConstant.DISABLE){
            //停售的也得把套餐中的停售
            //根据菜品id查询套餐
            List<Long> ids = setmealDishMapper.findByIds(Collections.singletonList(id));
            //如果套餐关联菜品，则将套餐也停售
            if (ids != null && ids.size() >0){
                for (Long setmealId : ids) {
                    setmealMapper.startOrStop(status,setmealId);
                }
            }

        }
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {

        //属性拷贝
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //菜品添加
        dishMapper.saveDish(dish);

        //Dish dis = dishMapper.findById();

        //获取菜品id
        Long dishId = dish.getId();

        //获取口味
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors == null || flavors.size() == 0){
            return;
        }

        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }

        //口味添加
        dishFlavorMapper.addFlavor(flavors);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO findById(Long id) {
        //查询菜品
        Dish dish = dishMapper.findById(id);
        //查询口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.findById(id);

        //将查到的数据封装到DishDTO中
        DishVO dishVO = new DishVO();

        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {

        //属性拷贝
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //调用mapper实现修改

        dishMapper.update(dish);

        Long dishId = dish.getId();
        //删除口味
        dishFlavorMapper.deleteById(dishId);

        //获取菜品id，更具菜品id删除口味
        List<DishFlavor>  flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            //口味添加
            dishFlavorMapper.addFlavor(flavors);
        }

    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {

        //判断当前的菜品是否是在售中，如果是、则抛出异常
        for (Long id : ids) {
            Dish dish = dishMapper.findById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断单签的菜品是否关联套餐，如果是则不能删除
        List<Long> setmealId = setmealDishMapper.findByIds(ids);
        if (setmealId != null && setmealId.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        ids.forEach(id -> {
            //删除菜品口味
            dishFlavorMapper.deleteById(id);
            //删除菜品
            dishMapper.deleteById(id);
        });
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     */
    @Override
    public List<Dish> findCategoryById(Long categoryId) {
        List<Dish> dishList = dishMapper.findCategoryById(categoryId);
        return dishList;
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> findCategoryId(Long categoryId) {

        List<DishVO> dishVOList = new ArrayList<>();
        List<Dish> dishList = dishMapper.findCategoryById(categoryId);
        for (Dish dish : dishList) {
            Long dishId = dish.getId();
            List<DishFlavor> dishFlavors = dishFlavorMapper.findById(dishId);

            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            dishVO.setFlavors(dishFlavors);

            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
